/*
 * Copyright 2006 Matt Jensen
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */
package org.spectaql.internal;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.spectaql.*;
import org.spectaql.internal.proxy.cglib.CglibProxyInstantiationStrategy;

/**
 * <p>
 * <code>SpectaqlContextBuilderImpl</code> is the internal implementation of the {@link SpectaqlContextBuilder}
 * interface.
 * </p>
 * <p>
 * <strong>Thread Safety:</strong> Instances of this class are not thread safe. Configuration must be applied from a
 * single thread, but {@link SpectaqlContext} instances returned by {@link #create()} are safe for concurrent access.
 * </p>
 */
public class SpectaqlContextBuilderImpl implements SpectaqlContextBuilder
{
    private static Log LOG = LogFactory.getLog(SpectaqlContextBuilderImpl.class);

    /**
     * <p>
     * The configured proxy instantiation strategy. 
     * </p>
     */
    private ProxyInstantiationStrategy m_proxyInstantiationStrategy;

    /**
     * <p>
     * The configured persistence metadata source. 
     * </p>
     */
    private PersistenceAdapter m_persistenceAdapter;

    /**
     * <p>
     * Construct a {@link SpectaqlContextBuilderImpl} instance.
     * </p>
     */
    public SpectaqlContextBuilderImpl()
    {
        super();
    }

    /* inherit javadoc */
    public SpectaqlContext create()
    {
        validateAndApplyDefaults();
        final SpectaqlContext result = new SpectaqlContextImpl(m_proxyInstantiationStrategy, m_persistenceAdapter);
        if (LOG.isDebugEnabled())
        {
            LOG.debug("Returning context [" + result + "].");
        }
        return result;
    }

    /* inherit javadoc */
    public SpectaqlContextBuilder setProxyInstantiationStrategy(final ProxyInstantiationStrategy strategy)
            throws IllegalStateException
    {
        if (null == strategy)
        {
            throw new NullPointerException("The [strategy] argument cannot be null.");
        }
        else if (null != m_proxyInstantiationStrategy)
        {
            throw new IllegalStateException("The [proxyInstantiationStrategy] property has already been set.");
        }

        // Set the property and return this.
        m_proxyInstantiationStrategy = strategy;
        return this;
    }

    /* inherit javadoc */
    public SpectaqlContextBuilder setPersistenceAdapter(final PersistenceAdapter source) throws IllegalStateException
    {
        if (null == source)
        {
            throw new NullPointerException("The [source] argument cannot be null.");
        }
        else if (null != m_persistenceAdapter)
        {
            throw new IllegalStateException("The [persistenceAdapter] property has already been set.");
        }

        // Set the property and return this.
        m_persistenceAdapter = source;
        return this;
    }

    /**
     * <p>
     * Apply defaults for configuration items which were not specified by the client.  
     * </p>
     *
     * @throws IllegalStateException if a validation check fails.
     */
    private void validateAndApplyDefaults() throws IllegalStateException
    {
        if (null == m_proxyInstantiationStrategy)
        {
            m_proxyInstantiationStrategy = new CglibProxyInstantiationStrategy();
        }
        if (null == m_persistenceAdapter)
        {
            throw new IllegalStateException("The [persistenceAdapter] property must be specified.");
        }
    }
}
